package com.car.crawler;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.car.bean.XCarBrand;
import com.car.util.DocUtil;

public class XCarCarwl {

	// 得到口碑ID
	public List<Integer> getKBID() {
		List<Integer> kbids = new ArrayList<Integer>();
		Document doc = DocUtil.getDoc("http://newcar.xcar.com.cn/price/");
		Elements listCar = doc.select("div.container");
		for (Element menu : listCar) {
			Elements td = menu.select("tr td:not(.border-r)");
			for (Element car : td) {
				Elements li = car.select("li");
				for (Element a : li) {
					int xcar_kbid = Integer.parseInt(a.select("a").get(0).attr("href").split("/")[1]);
					String xcar_name = a.text();
					int xcar_bbsid = getBBSID(xcar_kbid);
					XCarBrand xcarBrand = new XCarBrand(xcar_name, xcar_kbid, xcar_bbsid);
					System.out.println(xcarBrand);
				}
			}
		}
		return kbids;
	}

	// 通过口碑ID得到bbsid
	public int getBBSID(int kbid) {
		int bbsid = 0;
		String url = "http://newcar.xcar.com.cn/" + kbid + "/";
		Document doc = DocUtil.getDoc(url);
		System.out.println("kb  " + url);
		Element li = doc.select("li.l_bbs a").get(2);
		String link = li.attr("href");
		bbsid = Integer.parseInt(link.split("=")[1]);
		return bbsid;
	}

	public static void main(String[] args) {
		XCarCarwl xcar = new XCarCarwl();
		xcar.getKBID();
	}
}
